package com.yt.Insurance.server.api.cms;

import com.yt.Insurance.models.dto.InsurancePolicyDTO;
import com.yt.Insurance.models.vo.InsurancePolicyVO;
import com.yt.Insurance.models.bo.PolicyQueryBO;
import com.yt.Insurance.models.service.InsurancePolicyService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;

// 添加用于接收审核参数的 DTO 类
class PolicyStatusDTO {
    private Integer statusAttrId;
    private String rejectReason;
    
    // Getters and Setters
    public Integer getStatusAttrId() {
        return statusAttrId;
    }
    
    public void setStatusAttrId(Integer statusAttrId) {
        this.statusAttrId = statusAttrId;
    }
    
    public String getRejectReason() {
        return rejectReason;
    }
    
    public void setRejectReason(String rejectReason) {
        this.rejectReason = rejectReason;
    }
}

/**
 * 管理员端保险投保接口
 */
@RestController
@RequestMapping("/api/admin/policy")
public class AdminInsurancePolicyController {

    @Autowired
    private InsurancePolicyService policyService;

    /**
     * 管理员创建投保单（代客户投保）
     */
    @PostMapping
    public ResponseEntity<InsurancePolicyVO> createPolicy(@Valid @RequestBody InsurancePolicyDTO policyDTO) {
        try {
            InsurancePolicyVO policyVO = policyService.createPolicy(policyDTO);
            return ResponseEntity.status(HttpStatus.CREATED).body(policyVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 根据ID获取保单详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<InsurancePolicyVO> getPolicyById(@PathVariable("id") Integer id) {
        try {
            InsurancePolicyVO policyVO = policyService.getPolicyById(id);
            if (policyVO != null) {
                return ResponseEntity.ok(policyVO);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 分页查询保单列表
     */
    @GetMapping("/list")
    public ResponseEntity<PageInfo<InsurancePolicyVO>> queryPolicyPage(PolicyQueryBO queryBO) {
        try {
            PageInfo<InsurancePolicyVO> pageInfo = policyService.queryPolicyPage(queryBO);
            return ResponseEntity.ok(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    /**
     * 审核保单
     */
    @PutMapping("/{id}/status")
    public ResponseEntity<String> updatePolicyStatus(@PathVariable("id") Integer policyId,
                                                     @RequestBody PolicyStatusDTO policyStatusDTO) {
        try {
            // 参数校验
            if (policyStatusDTO.getStatusAttrId() == null) {
                return ResponseEntity.badRequest().body("保单状态ID不能为空");
            }
            
            boolean result = policyService.updatePolicyStatus(policyId, policyStatusDTO.getStatusAttrId(), policyStatusDTO.getRejectReason());
            if (result) {
                return ResponseEntity.ok("保单状态更新成功");
            } else {
                return ResponseEntity.badRequest().body("保单状态更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误");
        }
    }

    /**
     * 退保接口
     */
    @PutMapping("/{id}/surrender")
    public ResponseEntity<String> surrenderPolicy(@PathVariable("id") Integer policyId,
                                                  @RequestBody(required = false) PolicySurrenderDTO policySurrenderDTO) {
        try {
            // 参数校验
            if (policySurrenderDTO == null) {
                return ResponseEntity.badRequest().body("请求参数不能为空");
            }
            
            if (policySurrenderDTO.getSurrenderReason() == null || policySurrenderDTO.getSurrenderReason().isEmpty()) {
                return ResponseEntity.badRequest().body("退保原因不能为空");
            }
            
            if (policySurrenderDTO.getRefundAmount() == null) {
                return ResponseEntity.badRequest().body("退款金额不能为空");
            }
            
            boolean result = policyService.surrenderPolicy(policyId, policySurrenderDTO.getSurrenderReason(), policySurrenderDTO.getRefundAmount());
            if (result) {
                return ResponseEntity.ok("退保成功");
            } else {
                return ResponseEntity.badRequest().body("退保失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误");
        }
    }
    
    // 添加用于接收退保参数的 DTO 类
    static class PolicySurrenderDTO {
        private String surrenderReason;
        private BigDecimal refundAmount;
        
        // Getters and Setters
        public String getSurrenderReason() {
            return surrenderReason;
        }
        
        public void setSurrenderReason(String surrenderReason) {
            this.surrenderReason = surrenderReason;
        }
        
        public BigDecimal getRefundAmount() {
            return refundAmount;
        }
        
        public void setRefundAmount(BigDecimal refundAmount) {
            this.refundAmount = refundAmount;
        }
    }
}